package com.coman404.mvc;

import com.coman404.mvc.conf.RequestDispatch;
import io.netty.channel.*;
import io.netty.handler.codec.http.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import java.util.HashMap;
import java.util.Map;

/**
 * @author NotFound404 at 2024/6/3
 * code code always code
 */

@Component
@ChannelHandler.Sharable
public class ControllerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    @Autowired
    private RequestDispatch requestDispatch;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest msg) throws Exception {
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        FullHttpResponse response1 = requestDispatch.dispatch(msg, response);
        ChannelFuture cf = channelHandlerContext.writeAndFlush(response1);
        cf.addListener(ChannelFutureListener.CLOSE);
    }
}
